<?php
//////////////////// Managing of Payrolls///////////////////////////

//***************************BANK ADVANCE
/*
Gets the html table for Bank Advance
*/
function manage_bank_adv_table($items,$controller,$all_totals)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('common_name'),
	$CI->lang->line('staffs_account_number'),
	$CI->lang->line('common_amount').' '.$controller->config->item('currency_inuse')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.="<tr><td colspan='20'></td></tr>";
	$table.=manage_bank_adv_table_data_rows($items,$controller,$all_totals);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function manage_bank_adv_table_data_rows($items,$controller,$all_totals)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=manage_bank_adv_item_data_row($sort,$controller);
	}				
	
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='20'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	else 
	{
		$table_data_rows.="<tr><td colspan='20'>&nbsp;</td></tr>";
		
		$table_data_rows.="<tr><td></td><td><strong>".$CI->lang->line('total')."</strong></td><td><strong>".number_format($all_totals['net_pay'], 2, '.', '')."</strong></td></tr>";
	}
	
	return $table_data_rows;
}

function manage_bank_adv_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	
	$table_data_row='<tr>';
	$table_data_row.='<td>'.$item[0].'</td>';
	$table_data_row.='<td>'.$item[1].'</td>';
	$table_data_row.='<td>'.number_format($item[2], 2, '.', '').'</td>';
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

//********************************************SSF
/*
Gets the html table to manage items.
*/
function manage_ssf_table($items,$controller,$all_totals)
{	
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('common_name'),
	$CI->lang->line('staffs_ss_no'),
	$CI->lang->line('staffs_basic_wage').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_to_ssf').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('total').' '.$controller->config->item('currency_inuse')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	
	$inner_values = array();
	foreach($items as $sort)
	{
		if(is_array($sort[3]))
		{
			foreach($sort[3] as $key => $value)
			{
				if(!array_key_exists("$key", $inner_values))
				{
					$inner_values[$key] = $key;
				}
			}
		}	
	}
	$str_head = '';
	if(!empty($inner_values))
	{
		foreach($inner_values as $key => $value)
		{
			$str_head .="$key % &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}
	}
	else
	{
		$str_head = '0 %';
	}
	
	
	$table.='</tr></thead><tbody>';
	
	$table.="<tr><td colspan='20'></td></tr>";
	$table.="<tr><td></td><td></td><td></td><td><strong>$str_head</strong></td><td><strong>".array_sum($inner_values)." %</strong></td></tr>";
	$table.=manage_ssf_table_data_rows($items,$controller,$all_totals);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function manage_ssf_table_data_rows($items,$controller,$all_totals)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=manage_ssf_item_data_row($sort,$controller);
	}				
	
	///////////////////////////////SSF TOTALS
	//defining the head
	$inner_values = array();
	foreach($items as $sort)
	{
		if(is_array($sort[3]))
		{
			foreach($sort[3] as $key => $value)
			{
				if(!array_key_exists("$key", $inner_values))
				{
					$inner_values[$key] = 0;
				}
			}
		}
	}
	foreach($items as $sort)
	{
		if(is_array($sort[3]))
		{
			foreach($sort[3] as $key => $value)
			{
				$inner_values[$key] += $value;
			}
		}
	}
	
	if(empty($inner_values))
	{
		$string_print  = number_format(0, 2, '.', '');
	}
	else
	{
		$string_print = "";
		foreach($inner_values as $key => $value)
		{
			$string_print .= number_format($value, 2, '.', '').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		}
	}
	
	///////////////////////////////////////////////////////////
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='20'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	else 
	{
		$table_data_rows.="<tr><td colspan='20'>&nbsp;</td></tr>";
		
		$table_data_rows.="<tr><td></td><td><strong>".$CI->lang->line('total')."</strong></td><td><strong>".number_format($all_totals['basic_wages'], 2, '.', '')."</strong></td><td><strong>$string_print</strong></td><td><strong>".number_format($all_totals['total_ssf_string'], 2, '.', '')."</strong></td></tr>";
	}
	
	return $table_data_rows;
}

function manage_ssf_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	
	$ssf_string = '';
	if(is_array($item[3]))
	{
		foreach($item[3] as $key => $value)
		{
			$ssf_string .= number_format($value, 2, '.', '').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		}
			
		$all_keys = 0;
		$all_values = 0;
		$total_ssf_string = '';
		foreach($item[3] as $key => $value)
		{
			$all_keys = $all_keys + $key;
			$all_values = $all_values + $value;
		}
		$total_ssf_string = number_format($all_values, 2, '.', '');
	}
	else
	{
		$ssf_string = number_format(0, 2, '.', '');
		$total_ssf_string  = number_format(0, 2, '.', '');
	}
	
	$table_data_row='<tr>';
	$table_data_row.='<td>'.$item[0].'</td>';
	$table_data_row.='<td>'.$item[1].'</td>';
	$table_data_row.='<td>'.number_format($item[2], 2, '.', '').'</td>';
	$table_data_row.='<td>'.$ssf_string.'</td>';
	$table_data_row.='<td>'.$total_ssf_string.'</td>';
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//*****************************************IRS
/*
Gets the html table to manage items.
*/
function manage_irs_table($items,$controller,$all_totals)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('common_name'),
	$CI->lang->line('staffs_position'),
	$CI->lang->line('staffs_basic_wage').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_allowances').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_emolument').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_total_ssf_payable').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_deductions').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_taxable_income').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_basic_tax').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_tax_to_irs').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_remarks')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.="<tr><td colspan='20'></td></tr>";
	$table.=manage_irs_table_data_rows($items,$controller,$all_totals);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function manage_irs_table_data_rows($items,$controller,$all_totals)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=manage_irs_item_data_row($sort,$controller);
	}				
	
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='20'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	else 
	{
		$table_data_rows.="<tr><td colspan='20'>&nbsp;</td></tr>";
		
		$table_data_rows.="<tr><td></td><td><strong>".$CI->lang->line('total')."</strong></td><td><strong>".number_format($all_totals['basic_wages'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['allowance_totals'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['emolument'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['total_ssf_payable'], 2, '.', '')."<strong/></td><td><strong>".number_format($all_totals['deduction_totals'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['taxable_income'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['tax'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['tax'], 2, '.', '')."</strong></td><td></td></tr>";
	}
	return $table_data_rows;
}

function manage_irs_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	
	$table_data_row='<tr>';
	$table_data_row.='<td>'.$item[0].'</td>';
	$table_data_row.='<td>'.$item[1].'</td>';
	$table_data_row.='<td>'.number_format($item[2], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[3], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[4], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[5], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[6], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[7], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[8], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[8], 2, '.', '').'</td>';
	$table_data_row.='<td>&nbsp;&nbsp;&nbsp;</td>';
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


//////////////////// *****************************Managing of Payrolls///////////////////////////

/*
Gets the html table to manage items.
*/
function manage_report_payroll_table($items,$controller,$all_totals)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array( 
	$CI->lang->line('common_name'),
	$CI->lang->line('staffs_basic_wage').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_allowances').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_net_wage').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_gross_pay').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_basic_tax').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_taxable_income').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_deductions').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_net_pay').' '.$controller->config->item('currency_inuse'),
	//$CI->lang->line('staffs_total_deductions').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('staffs_total_ssf_payable').' '.$controller->config->item('currency_inuse'),
	$CI->lang->line('common_modify')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.=manage_report_payroll_table_data_rows($items,$controller,$all_totals);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the items.
*/
function manage_report_payroll_table_data_rows($items,$controller,$all_totals)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($items as $sort)
	{
		$table_data_rows.=manage_report_payroll_item_data_row($sort,$controller);
	}				
	
	if(empty($items))
	{
		$table_data_rows.="<tr><td colspan='20'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('items_no_items_to_display')."</div></tr></tr>";
	}
	else 
	{
		$table_data_rows.="<tr><td colspan='20'>&nbsp;</td></tr>";
		
		$table_data_rows.="<tr><td><strong>".$CI->lang->line('total')."</strong></td><td><strong>".number_format($all_totals['basic_wages'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['gross_pay'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['net_pay'], 2, '.', '')."</strong></td><td><strong>".number_format($all_totals['total_ssf_payable'], 2, '.', '')."<strong/></td><td></td></tr>";
	}
	return $table_data_rows;
}

function manage_report_payroll_item_data_row($item,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);
	$item_id = $item[0];

	$width = $controller->get_form_width() + 300;
	$width2 = $controller->get_form_width();
	
	$table_data_row='<tr>';
	$table_data_row.='<td>'.$item[1].'</td>';
	$table_data_row.='<td>'.number_format($item[2], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[3], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[4], 2, '.', '').'</td>';
	$table_data_row.='<td>'.number_format($item[5], 2, '.', '').'</td>';
	$table_data_row.='<td>'.anchor($controller_name."/detailed_payroll/$item_id/width:$width2", $CI->lang->line('common_more'),array('class'=>'thickbox','title'=>$CI->lang->line('common_detailed_info'))).' &nbsp;&nbsp;'.anchor($controller_name."/print_payroll/$item_id/", $CI->lang->line('common_print'),array('class'=>'','title'=>$CI->lang->line('common_print'),'target'=>'blank')).'</td>';		
	$table_data_row.='</tr>';
	
	return $table_data_row;
}

/////////////**********************Printing of Staffs*********************** /////////////////////////

function staffs_print_manage_table($people,$controller)
{
	$CI =& get_instance();
	$table='<table class="tablesorter" id="sortable_table">';
	
	$headers = array(
	$CI->lang->line('common_last_name'),
	$CI->lang->line('common_first_name'),
	$CI->lang->line('staffs_id'),
	$CI->lang->line('staffs_position'),
	$CI->lang->line('common_email'),
	$CI->lang->line('common_phone_number')
	);
	
	$table.='<thead><tr>';
	foreach($headers as $header)
	{
		$table.="<th>$header</th>";
	}
	$table.='</tr></thead><tbody>';
	$table.="<tr><td colspan='20'></td></tr>";
	$table.=staffs_print_manage_table_data_rows($people,$controller);
	$table.='</tbody></table>';
	return $table;
}

/*
Gets the html data rows for the print.
*/
function staffs_print_manage_table_data_rows($people,$controller)
{
	$CI =& get_instance();
	$table_data_rows='';
	
	foreach($people->result() as $person)
	{
		$table_data_rows.=staffs_print_data_row($person,$controller);
	}
	
	if($people->num_rows()==0)
	{
		$table_data_rows.="<tr><td colspan='6'><div class='warning_message' style='padding:7px;'>".$CI->lang->line('common_no_persons_to_display')."</div></tr></tr>";
	}
	
	return $table_data_rows;
}

function staffs_print_data_row($person,$controller)
{
	$CI =& get_instance();
	$controller_name=$CI->uri->segment(1);

	$table_data_row='<tr>';
	$table_data_row.='<td>'.character_limiter($person->last_name,13).'</td>';
	$table_data_row.='<td>'.character_limiter($person->first_name,13).'</td>';
	$table_data_row.='<td>'.$person->account_number.'</td>';
	$table_data_row.='<td>'.character_limiter($person->position,20).'</td>';
	$table_data_row.='<td>'.mailto($person->email,character_limiter($person->email,22)).'</td>';
	$table_data_row.='<td>'.character_limiter($person->phone_number,13).'</td>';				
	$table_data_row.='</tr>';
	
	return $table_data_row;
}


?>